#include "fMain.h"
#include "ui_fMain.h"
#include "qscreen.h"
#include <QMoveEvent>
#include "qprocess.h"
#include <qdebug.h>
#include "QMouseEvent"
#include <QClipboard>
fMain::fMain(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::fMain)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    connect(ui->pushButtonOCR,SIGNAL(clicked(bool)),this,SLOT(onOCR()));
    connect(ui->pushButtonGrab,SIGNAL(clicked(bool)),this,SLOT(onGetGrabImage()));
    gw=new CGrabWidget();
    gw->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);
    connect((CGrabWidget*)gw,SIGNAL(sGrab(QRect)),this,SLOT(onGrabRect(QRect)));
    edit=new fTextEdit(this);
    edit->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);
    connect(this,SIGNAL(sShowContent()),this,SLOT(onShowContent()));
    connect(ui->pushButtonClose,SIGNAL(clicked(bool)),this,SLOT(onClose()));
    ui->comboBox->addItems(QStringList()<<"chi_sim"<<"eng");
}

fMain::~fMain()
{
    delete ui;
}

void fMain::onOCR()
{
    this->hide();
    gw->showMaximized();
    QEventLoop el;
    connect(this,SIGNAL(sGrabFinished()),&el,SLOT(quit()));
    el.exec();
    QString txt=callTesseract(toGray(QApplication::clipboard()->pixmap()));
    edit->setText(txt);
    this->show();
}

void fMain::onGrabRect(const QRect &rect)
{
    gw->hide();
    QScreen *screen=qApp->primaryScreen();
    QPixmap pm= screen->grabWindow(0,rect.left(),rect.top(),rect.width(),rect.height());
    QApplication::clipboard()->setPixmap(pm);
    emit sGrabFinished();
}





void fMain::onShowContent()
{
    QWidget*w=static_cast<QWidget*>(sender());
    if(!w->isHidden())
    {
        edit->resize(w->frameGeometry().width(),edit->height());
        edit->move(w->pos().x(),w->pos().y()+w->frameGeometry().height());
        if(!edit->text().isEmpty())
            edit->show();
    }
}

void fMain::onClose()
{
    this->close();
}

void fMain::onGetGrabImage()
{
    this->hide();
    gw->showMaximized();
    QEventLoop el;
    connect(this,SIGNAL(sGrabFinished()),&el,SLOT(quit()));
    el.exec();
    this->show();
}

QImage fMain::toGray(const QPixmap &pm)
{
    QImage org=pm.toImage();
    QImage dest(org.size(),QImage::Format_RGB888);
    int r,g,b;
    for(int i=0;i<org.width();i++)
    {
        for(int j=0;j<org.height();j++)
        {
            QRgb rgb= org.pixel(i,j);
            r=qRed(rgb);
            g=qGreen(rgb);
            b=qBlue(rgb);
            int v=(r*30+g*50+b*11)/100;
            dest.setPixel(i,j,qRgb(v,v,v));
        }
    }
    return dest;
}

QString fMain::callTesseract(const QImage &img)
{
    QString tmppath=".";//QStandardPaths::writableLocation(QStandardPaths::TempLocation);
    QString imgpath=QString("%1/tmp.png").arg(tmppath);
    img.save(imgpath);
    QString retpath=QString("%1/ret").arg(tmppath);
    QProcess process;
    QString program("tesseract.exe");
    QString target=ui->comboBox->currentText().trimmed();
    QStringList args=QStringList()<<imgpath<<retpath<<"-l"<<"chi_sim";

    process.start(program,args);
    QString retval;
    if(process.waitForFinished())
    {
        QFile fp(QString("%1.txt").arg(retpath));
        if(!fp.open(QFile::ReadOnly))
        {
            retval=QString("%1 open error:%2").arg(retpath).arg(fp.errorString());
        }
        else
        {
            retval=QString(fp.readAll());
        }
        fp.close();
    }
    else
    {
        retval=QString("%1 call failed:%2").arg(program).arg(process.errorString());
    }
    return retval;
}

bool fMain::event(QEvent *e)
{
    switch(e->type())
    {
    case QEvent::Expose:
    {

    }break;
    }
    return QWidget::event(e);
}

void fMain::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons()&Qt::LeftButton)
    {
        pLast=e->pos();
        this->move(pos()+(pLast-pDown));
    }
}

void fMain::mousePressEvent(QMouseEvent *e){
    if(e->buttons()&Qt::LeftButton)
    {
        pDown=e->pos();
    }
}

void fMain::mouseReleaseEvent(QMouseEvent *e){
    if(e->buttons()&Qt::LeftButton)
    {
        pLast=e->pos();
    }
}



void fMain::showEvent(QShowEvent *e)
{
    emit sShowContent();
}

void fMain::hideEvent(QHideEvent *e)
{
    edit->hide();
}

void fMain::moveEvent(QMoveEvent *e)
{
    QPoint vec= e->pos()-e->oldPos();
    edit->move(edit->pos()+vec);
}
